let getTime = require("../utils/getTime");
class FontApi {
  constructor() {}
  frontApi(router, mysql) {
    const koa2Req = require("koa2-request");
    router.get("/te", async (ctx, next) => {
      let maxid = await mysql.query(
        `select * from pay_list_one where id = (select max(id) from pay_list_one where openid ='oidHH5OcwQOyVqI8fCb6E3kYjrpA')`
      );
      // let isPayOne = await mysql.query(
      //   `select * from pay_list_one where openid ='${openid}'`
      // );

      console.log(maxid[0].id);
      ctx.body = {
        title: "koa2 test",
        maxid,
      };
    });
    // 获取小程序登录人员openid
    router.post("/get_openid", async (ctx, next) => {
      const appId = "w"; //微信官方注册开发者
      const AppSecret = "24de88e";

      // console.log(ctx.request.body);
      // console.log(ctx.query);
      // console.log(request.query); post不能这样用
      const { code } = ctx.request.body;
      console.log("接收到jscode1:", code);
      // const urls = `https://api.weixin.qq.com/sns/jscode2session?appid=${appId}&secret=${AppSecret}&js_code=${code}&grant_type=authorization_code`;
      // console.log(urls);
      const res = await koa2Req({
        url: `https://api.weixin.qq.com/sns/jscode2session?appid=${appId}&secret=${AppSecret}&js_code=${code}&grant_type=authorization_code`,
      });
      const bodys = JSON.parse(res.body);

      const session_key = bodys.session_key;
      const openid = bodys.openid;
      console.log("返回的session_key", bodys.session_key);
      console.log("返回openid:", bodys);
      ctx.body = {
        openid,
      };
    });
    // 暂时没用
    router.post("/set_openid", async (ctx, next) => {
      const { code } = ctx.request.body;
      // mysql.allPeopleTest(`'${code}'`).then((people) => {});
      let isId = await mysql.allPeopleTest(`'${code}'`);
      console.log(isId);
      console.log(isId[0].name);
      let mes = 1;
      if (isId.length === 0 || isId == null) {
        await mysql.query(`INSERT INTO all_people (openid) VALUES ('${code}')`);
        mes = 1; //注册
      } else {
        if (isId[0].name) {
          mes = 3;
        } else {
          mes = 2; //已经注册
        }
        // mes = 2; //已经注册
      }
      console.log("接收到jscode2:", code);
      // await mysql.query(`INSERT INTO all_people (openid) VALUES ('${code}')`);
      ctx.body = {
        mesg: mes,
      };
    });
    //扫码
    router.get("/sand_code", async (ctx, next) => {
      ctx.body = {
        mesg: "扫码成功",
      };
    });
    // 验证用户进度状态
    router.post("/test_openid", async (ctx, next) => {
      const { openid } = ctx.request.body;
      // mysql.allPeopleTest(`'${code}'`).then((people) => {});
      let isId = await mysql.allPeopleTest(`'${openid}'`); //检测all_people数据库存在该openid
      // console.log("ttttttttt");
      // console.log(isId);
      let mes = 1;
      // 不存在
      if (isId.length === 0 || isId == null) {
        // 检测是否在验证
        let infoIsId = await mysql.query(
          `select * from people_info where openid='${openid}'`
        );
        console.log(infoIsId);
        if (infoIsId.length === 0 || infoIsId == null) {
          mes = 1; //注册
        } else {
          mes = 2; //在验证
        }
      } // 存在
      else {
        mes = 3;
      }
      // console.log("接收到jscode2:", code);
      // await mysql.query(`INSERT INTO all_people (openid) VALUES ('${code}')`);
      ctx.body = {
        mesg: mes,
      };
    });
    // 注入无后台验证人员名单，人员信息注册
    router.post("/set_people_info", async (ctx, next) => {
      const { openid, name, age, gender, phoneNum, address } = ctx.request.body;

      mysql.query(
        `INSERT INTO people_info (openid,name,age,gender,phone,address) VALUES ('${openid}','${name}',${age},'${gender}','${phoneNum}','${address}')`
      );
      ctx.body = {
        mesg: "ok",
      };
    });
    // 根据openid获取人员信息
    router.post("/get_all_people", async (ctx, next) => {
      const { openid } = ctx.request.body;
      let data = await mysql.allPeopleTest(`'${openid}'`);
      ctx.body = {
        data,
      };
    });
    // 根据openid获取支付
    router.post("/get_pay_month", async (ctx, next) => {
      const { openid } = ctx.request.body;
      let data = await mysql.query(
        `select * from pay_month where openid ='${openid}'`
      );
      ctx.body = {
        data,
      };
    });
    // 根据openid修改支付状态，wate支付成功
    router.post("/set_pay_month_water", async (ctx, next) => {
      const { openid, finsh_time } = ctx.request.body;
      let up = await mysql.query(
        `update pay_month set water_status='1', finsh_time='${finsh_time}' where openid ='${openid}'`
      );

      ctx.body = {
        mesg: "ok",
      };
      // 获取人员信息
      let status = await mysql.query(
        `select * from pay_month where openid ='${openid}'`
      );
      let time = status[0].finsh_time;
      let name = status[0].name;
      let address = status[0].address;
      let water = status[0].water;
      let electric = status[0].electric;

      //注入个人支付记录;
      let isPayOne = await mysql.query(
        `select * from pay_list_one where openid ='${openid}'`
      );
      if (isPayOne.length === 0 || isPayOne == null) {
        await mysql.query(
          `INSERT INTO pay_list_one (openid,water_time,name,address,water) VALUES ('${openid}','${finsh_time}', '${name}','${address}','${water}')`
        );
      } else {
        let maxid = await mysql.query(
          `select * from pay_list_one where id = (select max(id) from pay_list_one where openid='${openid}')`
        );
        let id = isPayOne.length - 1;
        //最后一个数组
        if (isPayOne[id].water === null || isPayOne[id].water === undefined) {
          await mysql.query(
            `update pay_list_one set water='${water}',water_time='${finsh_time}' where id ='${maxid[0].id}'`
          );
        } else {
          await mysql.query(
            `INSERT INTO pay_list_one (openid,water_time,name,address,water) VALUES ('${openid}','${finsh_time}', '${name}','${address}','${water}')`
          );
        }
      }

      // 修改pay_month的最终支付状态，是否为2，注入pay_list
      if (status[0].ele_status == 1 && status[0].water_status == 1) {
        await mysql.query(
          `update pay_month set status='2' where openid ='${openid}'`
        );
        // let time = status[0].finsh_time;
        // let name = status[0].name;
        // let address = status[0].address;
        // let water = status[0].water;
        // let electric = status[0].electric;

        await mysql.query(
          `INSERT INTO pay_list (openid,time,name,address,water,electric) VALUES ('${openid}','${time}', '${name}','${address}','${water}','${electric}')`
        );
      }
    });
    // 根据openid修改支付状态，支付成功
    router.post("/set_pay_month_ele", async (ctx, next) => {
      const { openid, finsh_time } = ctx.request.body;
      let up = await mysql.query(
        `update pay_month set ele_status='1', finsh_time='${finsh_time}' where openid ='${openid}'`
      );
      // //注入个人支付记录;
      // await mysql.query(
      //   `INSERT INTO pay_list_one (openid,ele_time,water_time,name,address,water,electric) VALUES ('${openid}','${time}', '${name}','${address}','${water}','${electric}')`
      // );
      ctx.body = {
        mesg: "ok",
      };
      // 获取本月支付人员信息
      let status = await mysql.query(
        `select * from pay_month where openid ='${openid}'`
      );
      let time = status[0].finsh_time;
      let name = status[0].name;
      let address = status[0].address;
      let water = status[0].water;
      let electric = status[0].electric;

      //注入个人支付记录
      let isPayOne = await mysql.query(
        `select * from pay_list_one where openid ='${openid}'`
      );
      //判断数据库是否有该id，没有直接添加
      if (isPayOne.length === 0 || isPayOne == null) {
        await mysql.query(
          `INSERT INTO pay_list_one (openid,ele_time,name,address,electric) VALUES ('${openid}','${finsh_time}', '${name}','${address}','${electric}')`
        );
      } else {
        let maxid = await mysql.query(
          `select * from pay_list_one where id = (select max(id) from pay_list_one where openid='${openid}')`
        );
        //如果有，判断是否已经交过电费或水费
        let id = isPayOne.length - 1;
        //交过
        if (
          isPayOne[id].electric === null ||
          isPayOne[id].electric === undefined
        ) {
          await mysql.query(
            `update pay_list_one set electric='${electric}',ele_time='${finsh_time}' where id ='${maxid[0].id}'`
          );
        } else {
          await mysql.query(
            `INSERT INTO pay_list_one (openid,ele_time,name,address,electric) VALUES ('${openid}','${finsh_time}', '${name}','${address}','${electric}')`
          );
        }
      }

      //注入pay_list支付列表
      //如果没有一条记录，加入
      // if (isPayOne.length == 0 || isPayOne == null) {

      // } else {
      //   //如果有记录，修改注入最后一条
      //   let id = isPayOne[isPayOne.length];
      //   await mysql.query(
      //     `update pay_list_one set electric='${electric}' where id ='${id}'`
      //   );
      // }

      // 修改pay_month的最终支付状态，是否为2，注入pay_list
      if (status[0].ele_status == 1 && status[0].water_status == 1) {
        await mysql.query(
          `update pay_month set status='2' where openid ='${openid}'`
        );

        await mysql.query(
          `INSERT INTO pay_list (openid,time,name,address,water,electric) VALUES ('${openid}','${time}', '${name}','${address}','${water}','${electric}')`
        );

        // let isHave = await mysql.query(
        //   `select openid from pay_list where openid ='${openid}'`
        // );
        // //注入pay_list支付列表
        // if (isHave.length == 0 || isHave == null) {
        //   await mysql.query(
        //     `INSERT INTO pay_list (openid,time,name,address,water,electric) VALUES ('${openid}','${time}', '${name}','${address}','${water}','${electric}')`
        //   );
        // }
        // await mysql.query(
        //   `INSERT INTO pay_list (openid,time,name,address,water,electric) VALUES ('${openid}','${time}', '${name}','${address}','${water}','${electric}')`
        // );
      }
    });
    //举报，投诉信息
    router.post("/set_complaint", async (ctx, next) => {
      const { openid, name, state, date, phoneNumber, content } =
        ctx.request.body;

      // //注入个人投诉举报记录
      // await mysql.query(
      //   `INSERT INTO complaint (openid,ele_time,water_time,name,address,water,electric) VALUES ('${openid}','${time}', '${name}','${address}','${water}','${electric}')`
      // );
      //注入全部投诉举报记录
      await mysql.query(
        `INSERT INTO complaint (openid,name,state,date,phone,content,all_state) VALUES ('${openid}','${name}', '${state}', '${date}','${phoneNumber}','${content}','-1')`
      );
      ctx.body = {
        mesg: "ok",
      };
    });
    //报修信息注入
    router.post("/set_repaire", async (ctx, next) => {
      const { openid, name, state, date, phoneNumber, address, content } =
        ctx.request.body;

      // //注入个人投诉举报记录
      // await mysql.query(
      //   `INSERT INTO complaint (openid,ele_time,water_time,name,address,water,electric) VALUES ('${openid}','${time}', '${name}','${address}','${water}','${electric}')`
      // );
      //注入全部投诉举报记录
      await mysql.query(
        `INSERT INTO repaire (openid,name,state,date,phone,content,all_state,address) VALUES ('${openid}','${name}', '${state}', '${date}','${phoneNumber}','${content}','-1','${address}')`
      );
      ctx.body = {
        mesg: "ok",
      };
    });
    //温度上报注入，修改上报状态，一天上报一次
    router.post("/set_tem_report", async (ctx, next) => {
      const { openid, name, age, gender, phoneNumber, address, tem, time } =
        ctx.request.body;
      let temData = await mysql.query(
        `select * from tem_today_report where openid ='${openid}'`
      );
      let mesg = "ok";
      if (temData.length === 0 || temData == null) {
        //  没有该用户信息,证明刚刚注册
        let people_info = await mysql.query(
          `select * from all_people where openid ='${openid}'`
        );
        // console.log(people_info[0].name);
        for (const i in people_info) {
          let name = people_info[i].name;
          let age = people_info[i].age;
          let gender = people_info[i].gender;
          let phone = people_info[i].phone;
          let address = people_info[i].address;
          let openid = people_info[i].openid;
          await mysql.query(
            `INSERT INTO tem_today_report (openid,name,age,gender,phone,address,tem,time,state) VALUES ('${openid}','${name}', '${age}', '${gender}','${phone}','${address}','-1','-1','-1')`
          );
        }
        await mysql.query(
          `UPDATE tem_today_report set tem ='${tem}',time='${time}',state ='1' where openid ='${openid}'`
        );
        //注入全部温度上报
        await mysql.query(
          `INSERT INTO tem_report_list (openid,name,age,gender,phone,address,tem,time,state) VALUES ('${openid}','${name}', '${age}', '${gender}','${phoneNumber}','${address}','${tem}','${time}','1')`
        );
      } else {
        if (temData[0].state < 0) {
          //修改上报状态
          await mysql.query(
            `UPDATE tem_today_report set tem ='${tem}',time='${time}',state ='1' where openid ='${openid}'`
          );
          //注入全部温度上报
          await mysql.query(
            `INSERT INTO tem_report_list (openid,name,age,gender,phone,address,tem,time,state) VALUES ('${openid}','${name}', '${age}', '${gender}','${phoneNumber}','${address}','${tem}','${time}','1')`
          );
        } else {
          mesg = "已上报";
        }
      }
      ctx.body = {
        mesg,
      };
    });
    // 户主状态检测
    router.post("/test_pay_apply", async (ctx, next) => {
      const { openid } = ctx.request.body;
      let isId = await mysql.query(
        `select * from all_needpay where openid='${openid}'`
      ); //检测all_people数据库存在该openid

      let mes = 1;
      // 不存在
      if (isId.length === 0 || isId == null) {
        // 检测是否在验证
        let infoIsId = await mysql.query(
          `select * from all_needpay_info where openid='${openid}'`
        );
        console.log(infoIsId);
        if (infoIsId.length === 0 || infoIsId == null) {
          mes = 1; //需要注册
        } else {
          mes = 2; //验证中
        }
      } // 存在
      else {
        mes = 3;
      }
      // console.log("接收到jscode2:", code);
      // await mysql.query(`INSERT INTO all_people (openid) VALUES ('${code}')`);
      ctx.body = {
        mesg: mes,
      };
    });
    //户主申请，pay-apply-info
    router.post("/set_pay_apply", async (ctx, next) => {
      const { openid, name, gender, phoneNumber, address, time } =
        ctx.request.body;
      //注入全部温度上报
      await mysql.query(
        `INSERT INTO all_needpay_info (openid,name,gender,phone,address,time) VALUES ('${openid}','${name}', '${gender}','${phoneNumber}','${address}','${time}')`
      );
      ctx.body = {
        mesg: "ok",
      };
    });
    // 根据openid获取个人举报记录
    router.post("/get_complaint", async (ctx, next) => {
      const { openid } = ctx.request.body;
      let data = await mysql.query(
        `select * from complaint where openid ='${openid}'`
      );
      ctx.body = {
        data,
      };
    });
    // 根据openid获取个人报修记录
    router.post("/get_repaire", async (ctx, next) => {
      const { openid } = ctx.request.body;
      let data = await mysql.query(
        `select * from repaire where openid ='${openid}'`
      );
      ctx.body = {
        data,
      };
    });
    // 根据openid获取个人温度上报记录
    router.post("/get_tem", async (ctx, next) => {
      const { openid } = ctx.request.body;
      let data = await mysql.query(
        `select * from tem_report_list where openid ='${openid}'`
      );
      ctx.body = {
        data,
      };
    });
    // 获取个人支付信息
    router.post("/get_pay_list_one", async (ctx, next) => {
      const { openid } = ctx.request.body;
      let data = await mysql.query(
        `select * from pay_list_one where openid ='${openid}'`
      );
      ctx.body = {
        data,
      };
    });
    // 获取单个题目信息
    router.post("/get_ques_con_list", async (ctx, next) => {
      const { qId } = ctx.request.body;
      let data = await mysql.query(
        `select * from ques_con_list where q_id ='${qId}'`
      );
      ctx.body = {
        data,
      };
    });
    // 问卷调查答题详情
    router.post("/set_ques_people_req", async (ctx, next) => {
      const { openid, qId, peopleSelect } = ctx.request.body;
      let fromData = JSON.parse(peopleSelect);
      //注入全部温度上报
      console.log(qId);
      for (const key in fromData) {
        // console.log(key);
        let sortArr = key.split(",");
        let choiceCon = fromData[key];
        let option_id = sortArr[1];
        // console.log(sortArr);
        if (sortArr[0] === "单") {
          await mysql.query(
            `INSERT INTO ques_people_rep (openid,q_id,option_id,sort,choice_con) VALUES ('${openid}','${qId}', '${option_id}','单','${choiceCon}')`
          );
        } else if (sortArr[0] === "多") {
          let NchoiceCon = JSON.stringify(choiceCon);
          await mysql.query(
            `INSERT INTO ques_people_rep (openid,q_id,option_id,sort,choice_con) VALUES ('${openid}','${qId}', '${option_id}','多','${NchoiceCon}')`
          );
        } else {
          await mysql.query(
            `INSERT INTO ques_people_rep (openid,q_id,option_id,sort,choice_con) VALUES ('${openid}','${qId}', '${option_id}','填','${choiceCon}')`
          );
        }
        // console.log(fromData[key]);
      }

      // await mysql.query(
      //   `INSERT INTO all_needpay_info (openid,name,gender,phone,address,time) VALUES ('${openid}','${name}', '${gender}','${phoneNumber}','${address}','${time}')`
      // );
      ctx.body = {
        mesg: "ok",
      };
    });

    // 根据openid获取个人温度上报记录
    router.post("/test_ques_people_req", async (ctx, next) => {
      const { openid, qId } = ctx.request.body;
      // console.log("55");
      // console.log(openid);
      // console.log(qId);
      let data = await mysql.query(
        `select * from ques_people_rep where openid ='${openid}' and q_id ='${qId}'`
      );
      let IsWrite = "";
      if (data.length > 0) {
        console.log("ffsf");
        IsWrite = "已填过";
      } else {
        IsWrite = "没填过";
      }
      console.log(data);
      ctx.body = {
        IsWrite,
      };
    });
    // 外出申请
    router.post("/set_go_out_go", async (ctx, next) => {
      const { openid, name, time, applyTime, phone, address, checkbox } =
        ctx.request.body;
      await mysql.query(
        `INSERT INTO go_out_go (openid,name,phone,trans,dest,time,apply_time,state) VALUES ('${openid}','${name}', '${phone}','${checkbox}','${address}','${time}','${applyTime}','0')`
      );
      ctx.body = {
        mesg: "ok",
      };
    });
    // 返回申请
    router.post("/set_go_out_out", async (ctx, next) => {
      const {
        openid,
        name,
        phone,
        trans,
        nowAddress,
        goTime,
        arriveTime,
        applyTime,
        way,
      } = ctx.request.body;
      await mysql.query(
        `INSERT INTO go_out_out (openid,name,phone,trans,now_address,go_time,arrive_time,apply_time,way,state) VALUES ('${openid}','${name}', '${phone}','${trans}','${nowAddress}','${goTime}','${arriveTime}','${applyTime}','${way}','0')`
      );
      ctx.body = {
        mesg: "ok",
      };
    });
    // 外出扫码
    router.post("/sand_go_out_go", async (ctx, next) => {
      const { openid, name, phone, address, state, tem } = ctx.request.body;
      const time = getTime.dateNow() + " " + getTime.timeNow();
      await mysql.query(
        `INSERT INTO go_out_go_list (openid,name,phone,dest,time,state,tem) VALUES ('${openid}','${name}', '${phone}','${address}','${time}','${state}','${tem}')`
      );
      ctx.body = {
        mesg: "ok",
      };
    });
    // 返回扫码
    router.post("/sand_go_out_out", async (ctx, next) => {
      const { openid, name, phone, address, state, tem } = ctx.request.body;
      const time = getTime.dateNow() + " " + getTime.timeNow();
      await mysql.query(
        `INSERT INTO go_out_out_list (openid,name,phone,now_address,arrive_time,state,tem) VALUES ('${openid}','${name}', '${phone}','${address}','${time}','${state}','${tem}')`
      );
      ctx.body = {
        mesg: "ok",
      };
    });
    // 根据openid获取个人外出申请记录
    router.post("/get_go_out_go", async (ctx, next) => {
      const { openid } = ctx.request.body;
      // console.log("55");
      // console.log(openid);
      // console.log(qId);
      let data = await mysql.query(
        `select * from go_out_go where openid ='${openid}'`
      );

      // console.log(data);
      ctx.body = {
        data,
      };
    });
    // 根据openid获取个人返回申请记录
    router.post("/get_go_out_out", async (ctx, next) => {
      const { openid } = ctx.request.body;
      // console.log("55");
      // console.log(openid);
      // console.log(qId);
      let data = await mysql.query(
        `select * from go_out_out where openid ='${openid}'`
      );

      // console.log(data);
      ctx.body = {
        data,
      };
    });
    //获取个人累计扫码出入次数
    router.post("/go_out_times", async (ctx, next) => {
      const { openid } = ctx.request.body;
      let goData = await mysql.query(
        `select * from go_out_go_list where openid='${openid}'`
      );
      let outData = await mysql.query(
        `select * from go_out_out_list where openid='${openid}'`
      );
      let goDataLength = goData.filter((s) => s.state != "申请");
      let outDataLength = outData.filter((s) => s.state != "申请");
      let goOutTimes = outDataLength.length + goDataLength.length;
      // console.log(goOutTimes);

      ctx.body = {
        mesg: "ok",
        goOutTimes,
      };
    });
    //获取个人出入记录
    router.post("/go_out_list", async (ctx, next) => {
      const { openid } = ctx.request.body;
      let goData = await mysql.query(
        `select * from go_out_go_list where openid='${openid}'`
      );
      let outData = await mysql.query(
        `select * from go_out_out_list where openid='${openid}'`
      );
      let goDataLength = goData.filter((s) => s.state != "申请");
      let outDataLength = outData.filter((s) => s.state != "申请");
      const data = [...goDataLength, ...outDataLength];
      // console.log(data);

      ctx.body = {
        mesg: "ok",
        data,
      };
    });
    // 购物车添加或修改数量，更新时间
    router.post("/set_cart_one", async (ctx, next) => {
      const { openid, sId, img } = ctx.request.body;
      // console.log("55");
      // console.log(openid);
      // console.log(qId);
      let data = await mysql.query(
        `select * from cart_one where openid ='${openid}' and s_id ='${sId}'`
      );
      let IsWrite = "";
      if (data.length > 0) {
        //更新时间，添加数量
        let upTime = getTime.dateNow() + " " + getTime.timeNow();
        await mysql.query(
          `update cart_one set num=num+1,add_time='${upTime}' where openid ='${openid}' and s_id ='${sId}'`
        );
        IsWrite = "已填过";
      } else {
        let addTime = getTime.dateNow() + " " + getTime.timeNow();
        await mysql.query(
          `insert into cart_one (openid, s_id,num,add_time,img) values('${openid}', '${sId}','1','${addTime}','${img}')`
        );
        IsWrite = "没填过";
      }
      // console.log(data);
      ctx.body = {
        IsWrite,
      };
    });
    // 购物车添加数量
    router.post("/set_cart_one_add", async (ctx, next) => {
      const { openid, sId } = ctx.request.body;
      await mysql.query(
        `update cart_one set num=num+1 where openid ='${openid}' and s_id ='${sId}'`
      );
      ctx.body = {
        mesg: "ok",
      };
    });
    // 购物车数量减少
    router.post("/set_cart_one_reduce", async (ctx, next) => {
      const { openid, sId } = ctx.request.body;
      await mysql.query(
        `update cart_one set num=num-1 where openid ='${openid}' and s_id ='${sId}'`
      );
      ctx.body = {
        mesg: "ok",
      };
    });
    // 购物车删除
    router.post("/set_cart_one_delete", async (ctx, next) => {
      const { openid, sId } = ctx.request.body;
      await mysql.query(
        `delete from cart_one where openid ='${openid}' and s_id ='${sId}'`
      );
      ctx.body = {
        mesg: "ok",
      };
    });
    // 获取购物车,从cart和cart_one组合数据
    router.post("/cart_my", async (ctx, next) => {
      const { openid } = ctx.request.body;
      let data = await mysql.query(
        `select * from cart_one where openid ='${openid}'`
      );
      let allData = [];
      for (let i = 0; i < data.length; i++) {
        let cartData = await mysql.query(
          `select price,title from cart where s_id = '${data[i].s_id}'`
        );
        let addPrice = cartData[0].price;
        let addTitle = cartData[0].title;

        allData.push({ ...data[i], price: addPrice, title: addTitle });

        // console.log(price);
      }
      // console.log(allData);
      ctx.body = {
        mesg: "ok",
        data: allData,
      };
    });
    // 获取购物车物品数量
    router.post("/cart_my_num", async (ctx, next) => {
      // console.log("55");
      const { openid } = ctx.request.body;
      let data = await mysql.query(
        `select * from cart_one where openid ='${openid}'`
      );
      // console.log(openid);
      // console.log(data.length);
      ctx.body = {
        mesg: "ok",
        data: data.length,
      };
    });
    // 购物确认订单
    router.post("/cart_order", async (ctx, next) => {
      // console.log("55");
      const { openid, name, orderData, money, phone, address } =
        ctx.request.body;
      let orderId = Math.random().toString().substr(3, 4) + Date.now();
      let addTime = getTime.dateNow() + " " + getTime.timeNow();
      // 生成[100000,999999]六位数随机取货码
      let pickup_code = Math.floor(
        Math.random() * (999999 - 100000 + 1) + 100000
      );
      await mysql.query(
        `insert into cart_order (order_id,pickup_code,openid,order_data,name,money,phone,address,time,status) values('${orderId}','${pickup_code}','${openid}','${orderData}','${name}','${money}','${phone}','${address}','${addTime}','-1')`
      );
      ctx.body = {
        mesg: "ok",
        // data: data.length,
      };
    });
    // 用户确认收货
    router.post("/cart_order_recive", async (ctx, next) => {
      // console.log("55");
      const { id } = ctx.request.body;
      // console.log("tuwen");
      let over_time = getTime.dateNow() + " " + getTime.timeNow();
      await mysql.query(
        `update cart_order set status='2',over_time='${over_time}' WHERE id = '${id}'`
      );
      ctx.body = {
        mesg: "ok",
      };
    });
    // 用户搜索商品
    router.post("/cart_search", async (ctx, next) => {
      // console.log("55");
      const { value } = ctx.request.body;
      let data = await mysql.query(
        `select * from cart where title like '%${value}%'`
      );
      ctx.body = {
        mesg: "ok",
        data,
      };
    });
    // 用户获取自己订单信息
    router.post("/cart_order_my", async (ctx, next) => {
      // console.log("55");
      const { openid } = ctx.request.body;
      let data = await mysql.query(
        `select * from cart_order where openid ='${openid}'`
      );
      ctx.body = {
        mesg: "ok",
        data,
      };
    });
    // 获取新闻信息
    router.get("/news", async (ctx, next) => {
      let data = await mysql.select("news");

      ctx.body = {
        mesg: "ok",
        data,
      };
    });
    //阅读界面获得单个新闻
    router.get("/news-id", async (ctx, next) => {
      let id = ctx.query;
      console.log(id.id);

      const sql = `select * from news where id=${id.id}`;
      let data = await mysql.query(sql);
      ctx.body = {
        mesg: "ok",
        data,
      };
    });
    // 获取防疫知识信息
    router.get("/prevention-knowledges", async (ctx, next) => {
      let data = await mysql.select("prevention_knowledge");

      ctx.body = {
        mesg: "ok",
        data,
      };
    });
    //阅读界面获得单个防疫知识
    router.get("/prevention-knowledge-id", async (ctx, next) => {
      let id = ctx.query;
      console.log(id.id);

      const sql = `select * from prevention_knowledge where id=${id.id}`;
      let data = await mysql.query(sql);
      ctx.body = {
        mesg: "ok",
        data,
      };
    });
    //问卷调查列表名字获取
    router.get("/ques-name-list", async (ctx, next) => {
      let data = await mysql.select("ques_name_list");
      // console.log(err);
      ctx.body = {
        mesg: "ok",
        data,
      };
    });
    //首页六个公告
    router.get("/notice-list6", async (ctx, next) => {
      let data = await mysql.query(
        "select * from notices order by id desc limit 6"
      );
      // console.log(err);
      ctx.body = {
        data,
        mesg: "ok",
      };
    });
    // 获取公告
    router.get("/notices", async (ctx, next) => {
      let data = await mysql.select("notices");

      ctx.body = {
        mesg: "ok",
        data,
      };
    });
    //阅读界面获得单个公告
    router.get("/notice-id", async (ctx, next) => {
      let id = ctx.query;
      // console.log(id.id);

      const sql = `select * from notices where id=${id.id}`;
      let data = await mysql.query(sql);
      ctx.body = {
        mesg: "ok",
        data,
      };
    });
    // 获取总购物各个分类表
    router.get("/cart", async (ctx, next) => {
      let data = await mysql.select("cart");

      ctx.body = {
        mesg: "ok",
        data,
      };
    });
    // 获取总购物商品列表
    router.get("/cart-sort", async (ctx, next) => {
      const { sort } = ctx.query;
      // console.log(sort);
      let data = await mysql.query(`select * from cart where sort ='${sort}'`);

      ctx.body = {
        mesg: "ok",
        data,
      };
    });
    // 获取单个商品
    router.get("/cart-detail", async (ctx, next) => {
      const { sId } = ctx.query;
      // console.log(sort);
      let data = await mysql.query(`select * from cart where s_id ='${sId}'`);

      ctx.body = {
        mesg: "ok",
        data,
      };
    });
  }
}
module.exports = new FontApi();
